<?php

if(isset($_GET['type'])) {
    $type = $_GET['type'];
    $next = null;
    if (isset($_GET['next']))
    {
        $next = $_GET['next'];
    }
    $rank;
    
    if (is_null($next))
    {
        $next = 0;
    }
        
    
    $useQuery = true;
    
    switch ($type) {
    case 0:
        $rank = "h_index";
        break;
    case 1:
        $rank = "g_index";
        break;
    case 2:
        $useQuery = false;
        break;
    }
    
    $db = mysql_connect('localhost', 'boughtns_user', 'GodLovesYou!');
    
    if (!$db) {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db('boughtns_mas', $db);
    
    if ($useQuery) {
        $query = "SELECT * FROM organization order by " . $rank . " desc LIMIT " . $next . ",10";
    }
    else
    {
         $query = "SELECT * FROM organization where h_index <= " . $next . " order by h_index desc LIMIT 20";
        
    }
   
    
   // echo $query;
    
    $result = mysql_query($query);
    echo mysql_error();
    
    $dataArray = array();
    
    while($row = mysql_fetch_assoc($result)) 
    {
        $id = $row['id'];
        $name = $row['name'];
        $hIdx = $row['h_index'];
        $gIdx = $row['g_index'];
        $json = new jsonObj;
        $json->id = $id;
        $json->name = $name;
        $json->hIdx = $hIdx;
        $json->gIdx = $gIdx;
        array_push($dataArray,$json);
    }
    
    
    
    echo json_encode($dataArray);
    
    mysql_close($db);
}



class jsonObj
{
    public $id;
    public $name;
    public $gIdx;
    public $hIdx;
    
}    

?>